package com.cl.short_video_java.util;


import cn.hutool.core.util.RandomUtil;
import cn.hutool.extra.mail.MailUtil;

import java.io.*;
import java.text.MessageFormat;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import com.cl.short_video_java.pojo.SysMailLog;
import com.cl.short_video_java.service.SysMailLogService;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @Author 付
 * @Date 2024-11-02 10:58
 * @Version 1.0
 * @description
 * @since 1.0
 */
@Component
public class MailUtils {
	private final static Logger logger = LoggerFactory.getLogger(MailUtil.class);

	/**
	 * Spring官方提供的集成邮件服务的实现类，目前是Java后端发送邮件和集成邮件服务的主流工具。
	 */
	@Resource
	private JavaMailSender mailSender;

	/**
	 * 从配置文件中注入发件人的姓名
	 */
	@Value("${spring.mail.username}")
	private String fromEmail;

	@Autowired
	private SysMailLogService sysMailLogService;

	@Autowired
	private FreeMarkerConfigurer freeMarkerConfigurer;

	@Autowired
	RedisUtil redisUtil;

	/**
	 * 发送文本邮件
	 *
	 * @param to      收件人
	 * @param subject 标题
	 * @param content 正文
	 */
	public void sendSimpleMail(String to, String subject, String content) {
		SimpleMailMessage message = new SimpleMailMessage();
		//发件人
		message.setFrom(fromEmail);
		message.setTo(to);
		message.setSubject(subject);
		message.setText(content);
		mailSender.send(message);
	}

	/**
	 * 发送html邮件
	 */
	public void sendHtmlMail(String to, String subject, String content) {
		try {
			//注意这里使用的是MimeMessage
			MimeMessage message = mailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(message, true);
			helper.setFrom(fromEmail);
			helper.setTo(to);
			helper.setSubject(subject);
			//第二个参数：格式是否为html
			helper.setText(content, true);
			mailSender.send(message);
		} catch (MessagingException e) {
			logger.error("发送邮件时发生异常！", e);
		}
	}

	/**
	 * 发送模板邮件
	 *
	 * @param to
	 * @param subject
	 * @param template
	 */
	public void sendTemplateMail(String to, String subject, String template) {
		try {
			// 获得模板
			Template template1 = freeMarkerConfigurer.getConfiguration().getTemplate(template);
			int l = RandomUtil.randomInt(100000, 999999);
			// 使用Map作为数据模型，定义属性和值
			Map<String, Object> model = new HashMap<>();
			model.put("title", subject);
			model.put("code", l);
			model.put("mail", to);
			// 传入数据模型到模板，替代模板中的占位符，并将模板转化为html字符串
			String templateHtml = FreeMarkerTemplateUtils.processTemplateIntoString(template1, model);
			// 该方法本质上还是发送html邮件，调用之前发送html邮件的方法
			this.sendHtmlMail(to, subject, templateHtml);
			String key = "mail" + to;
			SysMailLog sysMailLog = new SysMailLog();
			sysMailLog.setSubject(subject);
			sysMailLog.setContent("发送验证码：" + l);
			sysMailLog.setCreateTime(LocalDateTime.now());
			sysMailLog.setSendMail(fromEmail);
			sysMailLog.setReceptionMail(to);
			sysMailLogService.save(sysMailLog);
			// 10分钟过期
			redisUtil.set(key, String.valueOf(l), 5 * 60, TimeUnit.SECONDS);
		} catch (TemplateException e) {
			logger.error("发送邮件时发生异常！", e);
		} catch (IOException e) {
			logger.error("发送邮件时发生异常！", e);
		}
	}

	/**
	 * 发送带附件的邮件
	 *
	 * @param to
	 * @param subject
	 * @param content
	 * @param filePath
	 */
	public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
		try {
			MimeMessage message = mailSender.createMimeMessage();
			//要带附件第二个参数设为true
			MimeMessageHelper helper = new MimeMessageHelper(message, true);
			helper.setFrom(fromEmail);
			helper.setTo(to);
			helper.setSubject(subject);
			helper.setText(content, true);

			FileSystemResource file = new FileSystemResource(new File(filePath));
			String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
			helper.addAttachment(fileName, file);
			mailSender.send(message);
		} catch (MessagingException e) {
			logger.error("发送邮件时发生异常！", e);
		}

	}

}